import { createRouter, createWebHashHistory } from 'vue-router';
import LoginPage from '../components/LoginPage.vue';
import HomePage from '../components/HomePage.vue';
import { useAccountStore } from './AccountStore';

// 定义页面名称枚举
export enum PageName {
    login = 'login',
    home = 'home'
}
// 定义路由
const shopRouter = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/login',
            name: PageName.login,
            component: LoginPage,
        }, {
            path: '/home',
            name: PageName.home,
            component: HomePage,
        }
    ]
});
// 定义前置守卫
shopRouter.beforeEach((to) => {
    const accountStore = useAccountStore();
    
    if (to.name !== PageName.login && (!accountStore.isLogin || accountStore.userInfo?.role !== 'admin')) {
        return {
            name: PageName.login
        }
    }
});
// 导出路由
export default shopRouter;
